<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/admin.css">
    <title>许久没道过早安的后台</title>
    <style>
        .layui-card-header{
            height:auto;
            padding-top:15px;
            padding-bottom:10px;
        }
        .layui-inline{
            margin-right:10px;
        }
        #add{
            float:right;
        }
    </style>
</head>
<body>
<div class="body">
    <div class="layui-fluid">
        {include file='public/sider' /}
        <div id="main">
            {include file='public/header' /}

            <div class="layui-fluid" id="body">

                <div class="layui-card">
                    <div class="layui-card-header">
                        <div class="layui-form">
                            <div class="layui-inline">
                                <label class="layui-form-label">标题</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="title" placeholder="请输入" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">文章分类</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="label" placeholder="请输入" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn" lay-submit lay-filter="form">
                                    <i class="layui-icon layui-icon-search"></i>
                                </button>
                            </div>
                            <div class="layui-inline" id="add">
                                <button class="layui-btn">添加</button>
                            </div>
                        </div>
                    </div>
                    <div class="layui-card-body">
                        <table id="article_lst" lay-filter="test"></table>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<script src="/static/layui/layui.js"></script>
<script type="text/html" id="bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    var height = window.screen.availHeight;
    document.getElementById('body').style.height = parseInt(height) - 190 + "px";
    document.getElementById('sider').style.height = parseInt(height) - 165 + "px";
    var add_btn = document.getElementById('add');
    function add()
    {
        window.location.href= "{:url('article/add')}";
    }
    add_btn.onclick = add;
    layui.use(['jquery','table','layer','form',],function(){
        var $ = layui.jquery;
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        form.on('submit(form)',function(data){
            console.log(data.field);
            return false;
        });
        table.render({
            elem: '#article_lst',
            height: 600,
            url: "{:url('article/article_lst')}",
            page: true,
            limit: 10,
            cols:[[
                {field:'id',title:'ID',width:100,fixed:'left'},
                {field:'title',title:'文章标题'},
                {field:'label',title:'文章分类',width:300},
                {field:'click_times',title:'点击量',width:100,sort:true},
                {field:'status',title:'是否发布',width:120,sort:true},
                {field:'add_time',title:'添加时间',width:200,sort:true},
                {title:'操作',fixed: 'right', width: 150, toolbar: '#bar'}
            ]]
        });
        table.on('tool(test)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            if (layEvent === 'edit'){
                window.location.href="{:url('article/article_edit')}"+"?id="+obj.data['id'];
            }

            if (layEvent === 'del') { //删除
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    $.ajax({
                        async: true,
                        type: "POST",
                        url: "{:url('article/article_del')}",
                        data: obj.data,
                    })
                });
            }
        });
    });
</script>
</body>
</html>